/**
 * 快乐8的规则(基类)
 */
var kl8Rule = function() {
    this.map = {

    };
    var arr = ['大', '小', '单', '双', '810'];
    var arr2 = [1.99, 1.99, 1.99, 1.99, 108];
    for (var i in arr) {
        this.map['和' + arr[i]] = {
            display: '总和 - ' + arr[i],
            rate: arr2[i],
        }
    }
    var arr = ['大', '小'];
    var arr2 = ['单', '双'];
    for (var i in arr) {
        for (var j in arr2) {
            this.map["和" + arr[i] + arr2[j]] = {
                display: "总" + arr[i] + arr2[j],
                rate: 3.75,
            }
        }
    }

    this.map['前'] = {
        display: '前（多）',
        rate: 1.988,
    }
    this.map['后'] = {
        display: '后（多）',
        rate: 1.988,
    }
    this.map['中'] = {
        display: '前后（和）',
        rate: 3.82,
    }

    this.map['单'] = {
        display: '单（多）',
        rate: 1.988,
    }
    this.map['双'] = {
        display: '双（多）',
        rate: 1.988,
    }
    this.map['平'] = {
        display: '单双（和）',
        rate: 3.82,
    }

    var arr = ['金', '木', '水', '火', '土'];
    var arr2 = [9.21, 4.61, 2.41, 4.61, 9.21];
    for (var i in arr) {
        this.map[arr[i]] = {
            display: arr[i],
            rate: arr2[i],
        }
    }

    // 正码
    this.map["正"] = {
        display: "正码",
        rate: 3.81,
    }
};
kl8Rule.prototype = {};

kl8Rule.prototype.isHeOdd = function(list) {
    var total = this.he(list);
    return total % 2 == 1;
}
kl8Rule.prototype.isHeBig = function(list) {
    var total = this.he(list);
    return total > 810;
}
kl8Rule.prototype.isHeSmall = function(list) {
    var total = this.he(list);
    return total < 810;
}
kl8Rule.prototype.isHe810 = function(list) {
    var total = this.he(list);
    return total == 810;
}
kl8Rule.prototype.isQianDuo = function(list) {
    var t1 = 0, t2 = 0;
    for (var i in list) {
        var v = parseInt(list[i]);
        if (v > 40) t2++;
        else t1++;
    }
    return t1 > t2;
}
kl8Rule.prototype.isHouDuo = function(list) {
    var t1 = 0, t2 = 0;
    for (var i in list) {
        var v = parseInt(list[i]);
        if (v > 40) t2++;
        else t1++;
    }
    return t1 < t2;
}
kl8Rule.prototype.isQianHouHe = function(list) {
    var t1 = 0, t2 = 0;
    for (var i in list) {
        var v = parseInt(list[i]);
        if (v > 40) t2++;
        else t1++;
    }
    return t1 == t2;
}
kl8Rule.prototype.isDanDuo = function(list) {
    var t1 = 0, t2 = 0;
    for (var i in list) {
        var v = parseInt(list[i]);
        if (v % 2 == 0) t2++;
        else t1++;
    }
    return t1 > t2;
}
kl8Rule.prototype.isShuangDuo = function(list) {
    var t1 = 0, t2 = 0;
    for (var i in list) {
        var v = parseInt(list[i]);
        if (v % 2 == 0) t2++;
        else t1++;
    }
    return t1 < t2;
}
kl8Rule.prototype.isDanShuangHe = function(list) {
    var t1 = 0, t2 = 0;
    for (var i in list) {
        var v = parseInt(list[i]);
        if (v % 2 == 0) t2++;
        else t1++;
    }
    return t1 == t2;
}
kl8Rule.prototype.isJin = function(list) {
    var total = this.he(list);
    return total >= 210 && total <= 695;
}
kl8Rule.prototype.isMu = function(list) {
    var total = this.he(list);
    return total >= 696 && total <= 763;
}
kl8Rule.prototype.isShui = function(list) {
    var total = this.he(list);
    return total >= 764 && total <= 855;
}
kl8Rule.prototype.isHuo = function(list) {
    var total = this.he(list);
    return total >= 856 && total <= 923;
}
kl8Rule.prototype.isTu = function(list) {
    var total = this.he(list);
    return total >= 924 && total <= 1410;
}

// 计算总和
kl8Rule.prototype.he = function(list) {
    var total = 0;
    for (var i in list) {
        total += parseInt(list[i]);
    }
    return total;
}

/**
 * 计算露珠
 */
kl8Rule.prototype.luzhu = function(list, qiu, key) {
    // 将不是今天的开奖过滤掉
    var list2 = [];
    var today = new Date().Format("yyyy-MM-dd");
    for (var i in list) {
        if (list[i].time.startsWith(today)) list2.push(list[i]);
    }

    var self = this;
    if (key == "he") {
        // 总和
        var m = [];
        for (var i in list2) {
            var n = self.he(list2[i].red);
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "daxiao") {
        // 大小
        var m = [];
        for (var i in list2) {
            var n = "和";
            if (self.isHeBig(list2[i].red)) n = "大";
            else if (self.isHeSmall(list2[i].red)) n = "小";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "danshuang") {
        // 单双
        var m = [];
        for (var i in list2) {
            var n = self.he(list2[i].red) % 2 == 0 ? "双" : "单";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "wuxing") {
        // 五行
        var m = [];
        for (var i in list2) {
            var n = '';
            if (self.isJin(list2[i].red)) n = '金';
            else if (self.isMu(list2[i].red)) n = '木';
            else if (self.isShui(list2[i].red)) n = '水';
            else if (self.isHuo(list2[i].red)) n = '火';
            else if (self.isTu(list2[i].red)) n = '土';
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "qianhouhe") {
        // 前后和
        var m = [];
        for (var i in list2) {
            var n = '';
            if (self.isQianDuo(list2[i].red)) n = '前';
            else if (self.isHouDuo(list2[i].red)) n = '后';
            else n = '和';
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "danshuanghe") {
        // 单双和
        var m = [];
        for (var i in list2) {
            var n = '';
            if (self.isDanDuo(list2[i].red)) n = '单';
            else if (self.isShuangDuo(list2[i].red)) n = '双';
            else n = '和';
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
}

/**
 * 计算长龙信息
 */
kl8Rule.prototype.long = function(list) {
    var self = this;
    var r = [];
    if (isEmpty(list)) return r;

    // 总和: 大、小、单、双
    var l0 = list[0];

    var f = function(k, cb) {
        var t = 1;
        for (var i = 1; i < list.length; i++) {
            var l = list[i];
            if (!cb(l)) break;
            t++;
        }
        var oo = {}; oo[k] = t;
        if (t > 2) r.push(oo);
    };

    if (self.isHeBig(l0)) {
        f("总和 - 大", function(l) {
            return self.isHeBig(l);
        });
    }
    else if (self.isHeSmall(l0)) {
        f("总和 - 小", function(l) {
            return self.isHeSmall(l);
        });
    }
    if (self.isHeOdd(l0)) {
        f("总和 - 单", function(l) {
            return self.isHeOdd(l);
        });
    }
    else {
        f("总和 - 双", function(l) {
            return !self.isHeOdd(l);
        });
    }

    if (self.isQianDuo(l0))
        f("前（多）", function(l) {
            return self.isQianDuo(l);
        });
    else if (self.isHouDuo(l0))
        f("后（多）", function(l) {
            return self.isHouDuo(l);
        });
    else
        f("前后（和）", function(l) {
            return self.isQianHouHe(l);
        });
    if (self.isDanDuo(l0))
        f("单（多）", function(l) {
            return self.isDanDuo(l);
        });
    else if (self.isShuangDuo(l0))
        f("双（多）", function(l) {
            return self.isShuangDuo(l);
        });
    else
        f("单双（和）", function(l) {
            return self.isDanShuangHe(l);
        });

    if (self.isJin(l0))
        f("金", function(l) {
            return self.isJin(l);
        });
    else if (self.isMu(l0))
        f("木", function(l) {
            return self.isMu(l);
        });
    else if (self.isShui(l0))
        f("水", function(l) {
            return self.isShui(l);
        });
    else if (self.isHuo(l0))
        f("火", function(l) {
            return self.isHuo(l);
        });
    else if (self.isTu(l0))
        f("土", function(l) {
            return self.isTu(l);
        });

    // 排序
    r.sort(function(a, b) {
        var v1, v2;
        for (var k in a) v1 = a[k];
        for (var k in b) v2 = b[k];
        return v2 - v1;
    });
    return r;
}

/**
 * 转换显示的名称
 */
kl8Rule.prototype.getTargetName = function(name) {
    var arr = name.split(":");
    if (arr.length == 1)
        return this.map[name].display;
    return this.map[arr[0]].display + " &nbsp;<span class='blue' style='font-size:12px;'>" + arr[1] + "</span>";
};
